import { CateInfoAction } from '../actions'
import { CateInfo } from '../Types'
import { 
  GetCateInfoList_PENDING, 
  GetCateInfoList_FULFILLED, 
  GetCateInfoList_PAGE, 
  RECORD_Data_Pic_FULFILLED,
  GetCatePicList_PENDING, 
  GetCatePicList_FULFILLED,
  GetCatePicList_Type } from '../Constants'
// 初始化
const initState= {
  loading: false,
  record: {},
  type: 0,
  list: [],
  data: {
    list: [],
    pagination: {}
  }
}
export function CateInfoReducer(state: CateInfo = initState, action: CateInfoAction): CateInfo {
  switch (action.type) {
    case GetCateInfoList_PENDING:
      return { ...state, loading: true }
    case GetCateInfoList_FULFILLED:
      return { 
        ...state, 
        loading: false, 
        data: { 
          list: action.payload.list, 
          pagination: {
            ...state.data.pagination,
            total: action.payload.recordsTotal 
          } 
        } 
      }
    case GetCateInfoList_PAGE:
      return {
        ...state,
        data: { 
          pagination:{
            ...state.data.pagination,
            current: action.payload.current || 1,
            pageSize: action.payload.pageSize || 10
          }
        } 
      }
    case RECORD_Data_Pic_FULFILLED:
      return {
        ...state,
        record: action.payload
      }
    case GetCatePicList_PENDING:
      return { ...state, loading: true }
    case GetCatePicList_Type:
      return { ...state, type: action.payload }
    case GetCatePicList_FULFILLED:
      let list = state.list
      if (state.type == 1) list = action.payload.list
      else list = [
        ...state.list||[],
        ...action.payload.list
      ]
      return { 
        ...state, 
        loading: false, 
        list
      }

  }
  return state
}